/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Booking;

import common.*;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author cipher
 */
public class BookingPageBean extends PageBusiness {
    private Connection con;
    private BookingBean bb;


    public BookingPageBean() throws Exception {
        super();
        con = DbConnector.getConnection();
        v = new Vector();
    }

    @Override
    public int getAvailableCount() throws Exception {
        // TEMP: FOR TEST, SHOULD BE "FLIGHT"
        return this.getAvailableCountHelper(con,"AIRLINE");
    }

    @Override
    public PageBean listData(String page)throws Exception {
        PageBean pageBean = null;
        return pageBean;
    }

    public PageBean Proceed(int rv_id) throws Exception {
        try
        {//insert the code for confirmation result,
         //this require an insertion, an query and if cancelled by user then deletion

            PageBean pageBean = new PageBean(this);
            Statement stmt = con.createStatement();

            if (bb.getCardType() != null){
            //price needs to be added value
//            String InsertReservation = "insert into RESERVATION values (" + maxID + ",NULL, NULL, NULL," + bb.getPrice()  ;
                String iCardType;

            if (bb.getCardType().equals("0")){
                iCardType = "MASTERCARD";
            }else if (bb.getCardType().equals("1")) {
                iCardType = "VISA";
            }else {
                    iCardType = "AMERICANEXPRESS";
            }

                //bb.email should be replaced by session attribute
            String UpdateRv = "update RESERVATION set USERS_ID = " + bb.getEmail() + " where RESERVATION_ID = " + rv_id;
//            String InsertMRu = "insert into MAP_RESERVATION_USERS values (" + rv_id + ",'" +  bb.getEmail() + "')";
            //if this user doesn't exis, checked by a query of the user-email first
            String UpdateUsers = "update USERS set USERS_CREDIT_CARD_TYPE = '" + iCardType + "', USERS_CREDIT_CARD_NUMBER = '" + bb.getCardNumber() +
                    "', USERS_BILLING_ADDRESS = '" + bb.getAddr() + "', USERS_EXPIRATION_DATE = '" + bb.getExdate() +"' where USERS_EMAIL = '" + bb.getEmail() + "'";



//            String strSql = "select * from (select AIRLINE.*,rownum as rno from AIRLINE) where rno<=" + pageNum * pageBean.rowsPerPage;

//            ResultSet rset = stmt.executeQuery(InsertMRu);
            ResultSet rset = stmt.executeQuery(UpdateUsers);
            
            rset.close();
            stmt.close();
            
            }
            con.close();

            pageBean.data = null;
            pageBean.curPage = 0;

            return pageBean;
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    public Vector getResult()throws Exception
    {
        return v;
    }

    public BookingBean getBb () {
        return bb;
    }

    public void setBb (BookingBean bb) {
        this.bb = bb;
    }


}


